package com.xmad.ksadutils.AdSDK.AdMold.Kwai;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsDrawAd;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.xmad.ksadutils.AdSDK.AdBase.AdBaseLoad;
import com.xmad.ksadutils.AdSDK.AdBase.AdCode;
import com.xmad.ksadutils.AdSDK.Interface.AdLoad;

import java.util.List;

public class KsLoadDrawAd extends AdBaseLoad implements AdLoad {
    private volatile static KsLoadDrawAd mInstance;




    /**
     * 单例模式
     *
     * @return
     */
    public static KsLoadDrawAd getInstance() {
        if (mInstance == null) {
            synchronized (KsLoadDrawAd.class) {
                if (mInstance == null) {
                    mInstance = new KsLoadDrawAd();
                }
            }
        }
        return mInstance;
    }



    @Override
    public void Show(Activity activity, FrameLayout frameLayout) {
        if (KsAdId==null&& AdCode.getKsLoadDrawId()==0){
            Log.e(TAG,"KsLoadDrawAd————————还未设置ID:");
            return;
        }
        if (ToastCode){
            Log.e(TAG,KsAdId+"mmmmmmmmmmm");
            Toast.makeText(activity,KsAdId!=null ? KsAdId+"": AdCode.getKsLoadDrawId()+"",Toast.LENGTH_SHORT).show();
        }
        KsScene scene = new KsScene.Builder(KsAdId!=null ?  KsAdId:AdCode.getKsLoadDrawId()).adNum(1).build(); // 此为测试posId，请联系快手平台申请正式posId
        // 支持返回多条广告，默认1条，最多5条，参数范围1-5
        KsAdSDK.getLoadManager().loadDrawAd(scene, new KsLoadManager.DrawAdListener() {
            @Override
            public void onError(int code, String msg) {
                Log.e(TAG,"LoadDrawAd广告请求失败"+ code + msg);
            }
            @Override
            public void onDrawAdLoad(@Nullable List<KsDrawAd> adList) {
                if (adList == null || adList.isEmpty()) {
                    return;
                }
                KsDrawAd ksDrawAd=  adList.get(0);
                View drawVideoView = ksDrawAd.getDrawView(activity);
                if (drawVideoView != null && drawVideoView.getParent() == null) {
                    frameLayout.removeAllViews();
                    frameLayout.addView(drawVideoView);
                }
            }
        });
    }




}
